#include <map>


void testcase1_constKeyInMap()
{
    std::map<int, int> m;
    m[1] = 0;

    // get the address of the key
    const int* pKey = &m.begin()->first;

    // get the first element by reference. 
    // std::pair has the same template arruments as std::map above.
    // seem ok. But...
    const std::pair<int, int>& p = *(m.begin());
    const int* ppKey = &p.first;
    // the ppKey is different from pKey, because p is a reference of a 
    // temporary object. 

    // inside map, the key is const, so it saves std::pair<const int, int>
    // It is correctly way, no extra copy happening. 
    const std::pair<const int, int>& cp = *(m.begin());
    const int* pcpKey = &cp.first;

    // using auto, you don't need to worry about type mismatching. 
    auto& acp = *(m.begin());
    const int* pacpKey = &acp.first;
}



int Test_I05_PreferAutoToExplicitType(int, char**)
{
    testcase1_constKeyInMap();

    return 0;
}

/*
Things to Remember:
- auto variables must be initialized, are generally immune to type mismatches
  that can lead to portability or efficiency problems, can ease the process of re22
  factoring, and typically require less typing than variables with explicitly speci23
  fied types.
- auto-typed variables are subject to the pitfalls described in Items 2 and 6.
*/